<template>
  <div class="upload-body">
    <el-dialog v-model="centerDialogVisible" title="所有圈子" width="30%" center>
      <!-- 创建圈子弹框 -->
      <createCircle ref="createCircleRef"></createCircle>

      <div class="search-box f-j-sb-a-c">
        <div class="search-input f-a-c">
          <img class="svg-icon-search" src="@/assets/image/icon-search@2x.png" alt="" />
          <el-input v-model="searchValue" placeholder="输入名称搜索圈子"></el-input>
        </div>

        <div class="circle-btn add-circle-btn f-j-a-c" @click="showCreateCirclePop">
          <svg-icon class="svg-icon" name="icon-jq"></svg-icon>
          <span>建圈</span>
        </div>
      </div>


      <scrollMore maxHeight="400px" :noMore="true" :showMoreTitle="false">
        <div class="circle-list">
        <div
          class="circle-item f-j-sb-a-c"
          v-for="(item, index) in circleList"
          :key="index"
        >
          <img class="circle-img" :src="item.head_portrait" alt="" />
          <div class="circle-info">
            <div class="circle-name ellipsis">{{ item.circle_name }}</div>
            <div class="circle-num ellipsis">
              {{ item.posts_count }}条笔记 · {{ item.user_circle_count }}人讨论
            </div>
          </div>
          <div class="circle-btn select-circle-btn f-j-a-c" @click="confirm(item)">选择圈子</div>
        </div>

        <!-- 暂无数据 -->
        <noData v-if="!circleList.length"></noData>
      </div>
      </scrollMore>
    </el-dialog>
  </div>
</template>
<script>
import scrollMore from "@/components/scrollMore.vue";
import { circleByplateid, circleSearch } from "@/axios/community.js";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {
  defineComponent,
  reactive,
  toRefs,
  onUnmounted,
  onMounted,
  computed,
  getCurrentInstance,
  watch,
  ref,
} from "vue";
import createCircle from "./createCircle.vue";

export default defineComponent({
  components: {
    createCircle,
    scrollMore
  },
  setup(props, { attrs, slots, emit }) {
    const router = useRouter();
    const { proxy } = getCurrentInstance();
    const store = useStore();
    let createCircleRef = ref(null); //创建圈子弹框

    let data = reactive({
      centerDialogVisible: false,
      circleList: [], //圈子列表
      searchValue: "", //
      searchTimer: null,
    });

    // 监听弹框打开隐藏
    watch(
      () => data.centerDialogVisible,
      (newVal, oldVal) => {
        if (newVal) {
          getCircleByplateid();
        }
      }
    );

    // 监听输入
    watch(
      () => data.searchValue,
      (newVal, oldVal) => {
        clearTimeout(data.searchTimer);
        data.searchTimer = setTimeout(() => {
          if (data.searchValue) {
            getCircleSearch();
          } else {
            getCircleByplateid();
          }
        }, 500);
      }
    );

    // 获取最火圈子列表
    let getCircleByplateid = () => {
      circleByplateid().then((res) => {
        data.circleList = res.data;
      });
    };

    // 搜索圈子
    let getCircleSearch = () => {
      circleSearch({
        keyword: data.searchValue,
      }).then((res) => {
        data.circleList = res.data.data;
      });
    };

    // 建圈
    let showCreateCirclePop = () => {
      createCircleRef.value.open();
    };

    // 打开弹框
    let open = () => {
      data.centerDialogVisible = true;
    };

    // 取消
    let close = () => {
      data.centerDialogVisible = false;
    };

    // 确定
    let confirm = (item) => {
      // data.centerDialogVisible = false;
      // return data.voteList;
      console.log(item)
      emit("confirm",item);
      close();
    };

    return {
      showCreateCirclePop,
      createCircleRef,
      close,
      confirm,
      open,
      ...toRefs(data),
    };
  },
});
</script>
<style lang="scss" scoped>
.upload-body {
  :deep(.el-dialog.el-dialog--center) {
    border-radius: 20px;
    min-width: 520px;
    max-width: 600px;
  }

  :deep(.el-dialog--center .el-dialog__footer) {
    text-align: right !important;
  }
}

// 按钮公用样式
.circle-btn {
  font-size: 14px;
  color: #64c8dc;
  border: 1px solid #64c8dc;
  border-radius: 10px;
  width: 84px;
  height: 28px;
  cursor: pointer;
  &:active {
    opacity: 0.7;
  }
  .svg-icon {
    width: 24px;
    height: 24px;
    margin-right: 5px;
  }
}

.search-box {
  .search-input {
    flex: 1;
    position: relative;
    :deep(.el-input__wrapper) {
      background: #f5f6f7;
      border: none;
      height: 36px;
      border-radius: 10px;
      box-shadow: none;
      padding-left: 35px;
    }
    :deep(.el-input__wrapper.is-focus) {
      box-shadow: 0 0 0 1px #64c8dc inset;
    }
    .svg-icon-search {
      position: absolute;
      width: 18px;
      height: 18px;
      left: 10px;
      z-index: 10;
    }
  }

  .circle-btn {
    height: 36px;
    margin-left: 10px;
  }
}

// 列表
.circle-list {
  // max-height: 400px;
  // overflow-y: auto;
  margin-top: 10px;
  .circle-item {
    margin-top: 20px;
    .circle-img {
      width: 60px;
      height: 60px;
      border-radius: 50%;
      object-fit: cover;
    }
    .circle-info {
      flex: 1;
      padding: 0 20px;
      .circle-name {
        font-size: 14px;
        color: #000;
        font-weight: bold;
      }
      .circle-num {
        font-size: 14px;
        color: #656f79;
        padding-top: 10px;
      }
    }
  }
}
</style>
